package com.serviceImpl;

import java.util.List;
import java.util.Map;
import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.dao.manager.ElectricMapper;
import com.pojo.Electric;
import com.service.ElectricService;
import com.utils.UUIDUtils;
import com.utils.UploadUtils;

@Service
public class ElectricServiceImp implements ElectricService {

	@Resource
	private ElectricMapper electricMapper;
	@Value("${img.upload.path}")
	private String imgUploadPath;
	@Value("${img.access.path}")
	private String imgAccessPath;

	@Override
	public int addElectricInfo(Map<String, String> paramsMap, MultipartFile[] imgfiles) {
		try {
			String uuid = UUIDUtils.getUUID();
			//七牛上传图片
			List<String> imgPaths = UploadUtils.uploadQiniuImg(imgfiles, imgUploadPath, imgAccessPath);
			Electric entity = new Electric();
			if(imgPaths!=null && !imgPaths.isEmpty()) {
				entity.setImg(imgPaths.get(0));
			}else {
				entity.setImg("");
			}
			entity.setUuid(uuid);
			entity.setDeviceid(Integer.parseInt(paramsMap.get("deviceid")));
			entity.setPrinciple(paramsMap.get("principle"));
			entity.setElectrical(paramsMap.get("electrical"));
			electricMapper.addElectricInfo(entity);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public int deleteElectricInfo(Map<String, String> paramsMap) {
		try {
			return electricMapper.deleteElectricInfo(paramsMap);
		} catch (Exception e) {
			e.printStackTrace();
			return -2;
		}
	}

	@Override
	public int updateElectricInfo(Map<String, String> paramsMap, MultipartFile[] imgfiles) {
		try {
			List<String> imgPaths = UploadUtils.uploadQiniuImg(imgfiles, imgUploadPath, imgAccessPath);
			Electric entity = new Electric();
			if(imgPaths!=null && !imgPaths.isEmpty()) {
				entity.setImg(imgPaths.get(0));
			}
			entity.setUuid(paramsMap.get("uuid"));
			entity.setPrinciple(paramsMap.get("principle"));
			entity.setElectrical(paramsMap.get("electrical"));
			electricMapper.updateElectricInfo(entity);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public List<Map<String, String>> getElectricInfo(Map<String, String> paramsMap) {
		String page = paramsMap.get("page");
		if (page == null || "".equals(page)) {
			page = "1";
		}
		String num = paramsMap.get("num");
		if (num == null || "".equals(num)) {
			num = "10";
			paramsMap.put("num", num);
		}
		int start = (Integer.parseInt(page) - 1) * Integer.parseInt(num);
		paramsMap.put("start", start + "");
		return electricMapper.getElectricInfo(paramsMap);
	}

	@Override
	public Map<String, String> getElectricByDeviceId(Map<String, String> paramsMap) {
		return electricMapper.getElectricByDeviceId(paramsMap.get("id"));
	}

	@Override
	public List<Map<String, String>> getAllElectricInfo(Map<String, String> paramsMap) {
		// TODO Auto-generated method stub
		return electricMapper.getAllElectricInfo(paramsMap);
	}

}
